Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

window-or-global

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

window-or-global

Use either the window object in the browser or the global object in node.

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
112K
increased by8.33%
Maintainers
1
Weekly downloads
 
Created
Source

window-or-global

Use this module to get the global object both on server and client side. No more window is not defined errors just peace and flowers.

Advantages:

  • You can easily mock it by mockery or proxyquire in your tests
  • It's really useful in case of universal (or isomorphic) code, for example, when you'd like to render a React component both on client and server side

Install

$ npm i window-or-global

Usage

import root from 'window-or-global'

React example

components/my-component.jsx:

import React, { Component } from 'react'
// in node, you'll get the global object instead of crashing by an error
import root from 'window-or-global'

class MyComponent extends Component {

  // this method is only invoked in the browser environment
  componentDidMount() {
    root.addEventListener(/*...*/)
  }

  componentWillUnmount() {
    root.addEventListener(/*...*/)
  }

  render() {}

}

// Voilà. Enjoy your universal react component! ;)
// No more 'window is not defined' errors when you render your component
// on server side.

Unit-testing example

You can find a complete test example here. The test passes of course, just type

$ npm test

in your terminal.

License

MIT © Purpose Industries

Keywords

FAQs

Package last updated on 16 Sep 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc